package com.htpay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htpay.mapper.MerchantMapper;
import com.htpay.model.common.Constant;
import com.htpay.model.common.DictConstant;
import com.htpay.model.common.ResponseBean;
import com.htpay.model.entity.Merchant;
import com.htpay.service.IBaseAPIService;
import com.htpay.service.IMerchantService;
import com.htpay.util.ObjectConvertUtils;
import com.htpay.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Service
@Slf4j
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {

    @Autowired
    private MerchantMapper userMapper;
    @Autowired
    private IBaseAPIService sysBaseAPI;

    @Override
//    @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
    public ResponseBean resetPassword(String username, String oldpassword, String newpassword, String confirmpassword) {
        Merchant user = userMapper.getUserByName(username);
        String passwordEncode = PasswordUtil.encrypt(username, oldpassword, user.getSalt());
        if (!user.getPassword().equals(passwordEncode)) {
            return new ResponseBean(Constant.BUSI_ERROR_COD, "旧密码输入错误!", null);
        }
        if (ObjectConvertUtils.isEmpty(newpassword)) {
            return new ResponseBean(Constant.BUSI_ERROR_COD, "新密码不允许为空!", null);
        }
        if (!newpassword.equals(confirmpassword)) {
            return new ResponseBean(Constant.BUSI_ERROR_COD, "两次输入密码不一致!", null);
        }
        String password = PasswordUtil.encrypt(username, newpassword, user.getSalt());
        this.userMapper.update(new Merchant().setPassword(password), new LambdaQueryWrapper<Merchant>().eq(Merchant::getMerchantId, user.getMerchantId()));
        return new ResponseBean(Constant.BUSI_SUCC_CODE, "密码重置成功!", null);
    }

    @Override
//    @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
    public ResponseBean changePassword(Merchant merchant) {
//        String salt = ObjectConvertUtils.randomGen(8);
//        merchant.setSalt(salt);
//        String password = merchant.getPassword();
//        String passwordEncode = PasswordUtil.encrypt(merchant.getLoginName(), password, salt);
//        merchant.setPassword(passwordEncode);
//        this.userMapper.updateById(merchant);
//        return Result.ok("密码修改成功!");
        return null;
    }

    @Override
    public void updatePassword(Integer merchantId, String newPassword) {
        this.userMapper.update(new Merchant().setPassword(newPassword), new LambdaQueryWrapper<Merchant>().eq(Merchant::getMerchantId, merchantId));
    }

    @Override
    public void addUser(Merchant user) {
        this.save(user);
    }


    @Override
    public Merchant getUserByName(String username) {
        return userMapper.getUserByName(username);
    }


//    @Override
//    public SysUserCacheInfo getCacheUser(String username) {
//        SysUserCacheInfo info = new SysUserCacheInfo();
//
//        Merchant user = sysBaseAPI.getUserByName(username);
//        if (user != null) {
//            info.setSysUserCode(user.getLoginName());
//            info.setSysUserName(user.getRealName());
//        }
//
//        return info;
//    }

    @Override
    public Merchant getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }


    @Override
    public Merchant getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    /**
     * 校验用户是否有效
     *
     * @param merchant
     * @return
     */
    @Override
    public ResponseBean checkUserIsEffective(Merchant merchant) {
        //情况1：根据用户信息查询，该用户不存在
        if (merchant == null) {
            sysBaseAPI.addLog("用户登录失败，用户不存在！", DictConstant.LOG_TYPE_1, null);
            return new ResponseBean(Constant.BUSI_ERROR_COD, "该用户不存在，请注册", null);
        }
        //情况2：根据用户信息查询，该用户已注销
        if (DictConstant.DEL_FLAG_1.toString().equals(merchant.getDelFlag())) {
            sysBaseAPI.addLog("用户登录失败，用户名:" + merchant.getLoginName() + "已注销！", DictConstant.LOG_TYPE_1, null);
            return new ResponseBean(Constant.BUSI_ERROR_COD, "该用户已注销", null);
        }
        //情况3：根据用户信息查询，该用户已冻结
        if (DictConstant.USER_FREEZE.equals(merchant.getStatus())) {
            sysBaseAPI.addLog("用户登录失败，用户名:" + merchant.getLoginName() + "已冻结！", DictConstant.LOG_TYPE_1, null);
            return new ResponseBean(Constant.BUSI_ERROR_COD, "该用户已冻结", null);
        }

        ///返回有效用户信息
        Map<String, Merchant> merchantMap = new HashMap<String, Merchant>();
        merchantMap.put("userInfo", merchant);
        return new ResponseBean(Constant.BUSI_SUCC_CODE, "用户有效", merchantMap);
    }
}
